<template>
  <div class="app-container">
    <el-form label-width="120px" >
      <el-form-item label="banner名称">
        <el-input v-model="banner.title" />
      </el-form-item>
      
    <el-form-item label="添加时间">
        <el-date-picker type="datetime" placeholder="选择日期" v-model="banner.gmt_create" style="width: 100%;"></el-date-picker>
    </el-form-item>
      <!-- 图片地址 -->
      <el-form-item label="图片地址">
        <el-upload
            class="avatar-uploader"
            :action="BASE_API+'/eduoss/fileoss'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="banner.imageUrl" :src="banner.imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import banner from '../../../api/edu/banner';
export default {

    data(){
        return{
            BASE_API:process.env.BASE_API,
            banner:{
                id:'',
                title:'',
                imageUrl:'',
                gmtCreate:'',
            }
        }
    },
    methods:{
        beforeAvatarUpload(){
            const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
        },
        handleAvatarSuccess(res){
            this.banner.imageUrl = res.imageUrl
        },
        save(){
            banner.save(this.banner).then(res=>{
                //提示信息
                this.$message({
                type: "success",
                message: "添加成功",
                });
                //回到列表页面 路由跳转
                this.$router.push({ path: "/edu/banner/list" });
                    })
        },
        saveOrUpdate(){
            this.save()
        }
    }
}
</script>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>